package top.kaoshanji.p2ch15a;

/**
 * 代码清单-p2ch15a-440页a：多线程访问synchronized保护的Counter对象
 * 线程执行
 * author kaoshanji
 * ^_^
 * create time 2025/2/23 17:45
 */
public class CounterThread1 extends Thread {

    Counter counter;

    public CounterThread1(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            counter.incr();
        }
    }

    public static void main(String[] args) throws InterruptedException {

        int num = 100;
        Counter counter1 = new Counter();

        Thread[] threads = new Thread[num];
        for (int i = 0; i < num; i++) {
            threads[i] = new CounterThread1(counter1);
            threads[i].start();
        }

        for (int i = 0; i < num; i++) {
            threads[i].join();
        }

        System.out.println(counter1.getCount());

    }

}
